﻿using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Project;
using Microsoft.WowAddonStudio.Project;

namespace Microsoft.WowAddonStudio.TocEditor
{
	/// <summary>
	/// This class implements the metadata editing part of the TOC editor.
	/// </summary>
	[ComVisible(true)]
	[Guid(GuidStrings.TocEditorAddonInfoPage)]
	public class TocEditorAddOnInfoPage : SettingsPage
	{
		private MetadataManager manager;

		/// <summary>
		/// Creates instances of <see cref="TocEditorAddOnInfoPage"/>.
		/// </summary>
		public TocEditorAddOnInfoPage()
		{
			Name = Resources.GetString(Resources.AddOnInfoCaption);
		}

		/// <summary>
		/// The method gets called when the page is activated.
		/// </summary>
		public override void Activate(IntPtr parent, RECT[] pRect, int bModal)
		{
			base.Activate(parent, pRect, bModal);

			if (ProjectMgr is WowProjectNode)
			{
				manager = new MetadataManager((ProjectMgr as WowProjectNode).TocDocument);
				AddVisualElements();
				manager.OnMetadataChanged += manager_OnMetadataChanged;
			}
			else
			{
				throw new ApplicationException("Project manager type incompatibility error.");
			}
		}

		/// <summary>
		/// Gets called when the page is deactivated. This is the place where unsubscription from
		/// events happens.
		/// </summary>
		public override void Deactivate()
		{
			base.Deactivate();
			manager.OnMetadataChanged -= manager_OnMetadataChanged;
		}

		/// <summary>
		/// Binding hook. Not used.
		/// </summary>
		protected override void BindProperties()
		{
			//Nothing to bind here...
		}

		/// <summary>
		/// The method is invoked when the changes made by the user should be saved.
		/// </summary>
		protected override int ApplyChanges()
		{
			if (ProjectMgr is WowProjectNode)
			{
				if (IsDirty)
				{
					(ProjectMgr as WowProjectNode).SaveTocDocument();
					IsDirty = false;
				}

				return VSConstants.S_OK;
			}

			return VSConstants.E_INVALIDARG;
		}

		/// <summary>
		/// Handles the OnMetadataChanged event of the manager control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="Microsoft.WowAddonStudio.TocEditor.MetadataManagerEventArgs"/> instance containing the event data.</param>
		private void manager_OnMetadataChanged(object sender, MetadataManagerEventArgs e)
		{
			IsDirty = true;
		}

		/// <summary>
		/// Adds the visual elements.
		/// </summary>
		private void AddVisualElements()
		{
			var p = new Panel {Dock = DockStyle.Fill};
			ThePanel.Controls.Add(p);

			var basicControl = new BasicTocMetaEditorUserControl(manager);
			(ThePanel.Controls[0] as Panel).Controls.Add(basicControl);
			var extendedControl = new ExtendedTocMetaEditorUserControl(manager)
			                      	{
			                      		Location = new Point(basicControl.Location.X,
			                      		                     basicControl.Location.Y + basicControl.Height + 5)
			                      	};
			(ThePanel.Controls[0] as Panel).Controls.Add(extendedControl);
		}
	}
}